package com.foreknow.view;

import com.foreknow.service.impl.AdminServiceImpl;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

public class Login extends JFrame {

	private JPasswordField passwordField;
    private JPanel contentPane;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("用户名：");
        label.setFont(new Font("宋体", Font.PLAIN, 15));
        label.setBounds(46, 48, 66, 24);
        contentPane.add(label);

        JLabel label_1 = new JLabel("密码：");
        label_1.setFont(new Font("宋体", Font.PLAIN, 15));
        label_1.setBounds(46, 108, 66, 24);
        contentPane.add(label_1);

        textField = new JTextField();
        textField.setBounds(122, 50, 248, 22);
        contentPane.add(textField);
        textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(122, 108, 248, 22);
		contentPane.add(passwordField);

        JButton button = new JButton("登录");
        button.setBounds(61, 188, 66, 24);
        contentPane.add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

				AdminServiceImpl adminService = new AdminServiceImpl();
				String adminName = textField.getText();
				String password = new String(passwordField.getPassword());
				boolean isRight = adminService.login(adminName,password);
                if (isRight){
                    MainWindow mainw = new MainWindow();
                    //隐藏当前窗口
                    Login.this.dispose();
                }else {
                    textField.setText("");
                    passwordField.setText("");
                    JOptionPane jPanel = new JOptionPane();
                    JOptionPane.showMessageDialog(jPanel, "用户名或密码有误，请重新输入！", "提示",JOptionPane.WARNING_MESSAGE);
                }
            }

            public void login(ActionListener listener){

			}
        });

        JButton button_1 = new JButton("注册");
        button_1.setBounds(182, 188, 72, 24);
        contentPane.add(button_1);
        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Register register = new Register();
            }
        });

        JButton button_2 = new JButton("取消");
        button_2.setBounds(291, 188, 79, 24);
        contentPane.add(button_2);
        button_2.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //发送WINDOW_CLOSING的消息给frame，然后frame就会关闭，并且windowClosing的也会响应
                Login.this.dispatchEvent(new WindowEvent(Login.this,WindowEvent.WINDOW_CLOSING));
            }
        });
    }
}
